
 @include "lib.awk"
 @include "findsome.awk"
 @include "nbins.awk"
 @include "find.awk"
 @include "power.awk"
 
 function planner(queryString,best,rest,sorter,\
                  name,lo,hi,goals,d,w,has,at,powers,\
                  bests,rests,row,classes,i,n,active,neighborhood) {
   print "\n~~~~~~~~~~~~~~~~~~~~~~~" 
   print queryString "\n"
   bestsRests(queryString,best,rest,sorter,  
              name,lo,hi,goals,d,w,has,at,
              bests,rests)
   concat(bests,rests,neighborhood)
   keys(neighborhood,active)
   for(row in active)
       print (classes[row] = @FindClass(goals,row,d))
   print("BEST") ; dwrite(d,name,w,bests)
   print("REST") ; dwrite(d,name,w,rests,1)
   n = power(w,goals,bests,rests,d,powers) 
   howYaDone("baseline",length(active),classes)
   for(i=1;i<=n;i++) 
     howYaDoing(i,powers,at,active,goals,d,name)
 }
 function howYaDoing(i,powers,at,active,goals,d,name,  \
                     n,col,want,found,j,q,row,class,str,classes) {
   for(j=1;j<=i;j++) {
     col  = powers[j]["col"]
     want = powers[j]["="]
     q[col][want] = want
     str = str name[col] "=" want " "
   }
   n = find(q,found,at,active)
   for(j in found) {
     row = found[j]["id"]
     classes[j]  = @FindClass(goals,row,d)
     }
   howYaDone(str,n,classes)
 }
 function howYaDone(key,n,l) {
   printf("support, %3d, med, %5.1f, mid50, %5.1f, with, %s\n",
          n,median(l),middle50(l),key)
 }
 function bestsRests(queryString,best,rest,sorter,\
                     name,lo,hi,goals,d,w,has,at,\
                     bests,rests,\
                     active,n,rows0,rows,all,q,id,row,i) {
   query(queryString,name,q)
   keys(d,active)
   n = findSome(q,rows0,at, active)
   all = best + rest
   if (all > n) {
     best = int(best/all * n)
     all  = n
     rest = n - best
   }
   for(i= best+rest+1; i<= n; i++) 
     delete rows0[i]
   if (n > 8) {   
     for(row in rows0) {
       id = rows0[row]["id"]
       rows0[row]["class"] = @FindClass(goals,id,d)
     }
     asort(rows0,rows,sorter) 
     for(row=1;row<=all;row++) {
       id = rows[row]["id"]
       row <= best ? bests[id] = id : rests[id] = id
 }}}
 function asc_class(a,b,c,d) {
   return b["class"] - d["class"] 
 }
 # function power(w,goals,bests,rests,d,out,\
 #                good, bad,n,col,val,b,r,tmp,score,max,row) {
 #   counts(bests,w,goals,d,good)
 #   counts(rests,w,goals,d,bad)
 #   for(col in good) 
 #     for(val in good[col]) {
 #       b = good[col][val] 
 #       r = 0
 #       if (col in bad) 
 #         if(val in bad[col]) 
 #           r = bad[col][val] 
 #       b = b / length(bests)
 #       r = r / length(rests)
 #       if (b > r) {
 #         tmp[++n]["val"] = score = b^2/ (b+r)
 #         tmp[  n]["col"] = col
 #         tmp[  n]["="] = val
 #         if (score > max) max = score
 #       }
 #     }
 #   for(row in tmp) 
 #     if(tmp[row]["val"] < max/3)
 #       delete tmp[row]
 #   return asort(tmp,out,"valasc")
 # }  
 # function counts(rows,w,goals,d,count,   row,col,val) {
 #   for(col in w)
 #     if( ! (col in goals))
 #       for(row in rows)
 #         if ( (val = d[row][col]) !~ "?")
 #           count[col][val]++
 # }
   
